package cn.lopr.im.api.service.impl;

import cn.lopr.im.api.bean.Message;
import cn.lopr.im.api.dao.MessageMapper;
import cn.lopr.im.api.service.IMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shuangyang7
 * @date 2020/12/28 15:30
 */
@Service(value = "messageService")
public class MessageServiceImpl implements IMessageService {

    @Autowired
    MessageMapper messageMapper;

    @Override
    public boolean addMessage(Message message) {
        message.setId(null);

        if (message.getType().equals(Message.MessageType.SEND_ADD.getCode() + "")) {
            Message _msg = new Message().setType(Message.MessageType.SEND_ADD.getCode() + "")
                    .setToUser(message.getToUser()).setFromUser(message.getFromUser())
                    .setIsRead(0);
            if (messageMapper.selectByRecord(_msg).size() != 0) {
                return true;
            }
        }
        int insert = messageMapper.insert(message);
        return insert != 0;
    }

    @Override
    public List<Message> getNotReadMessage(String phone) {
        Message message = new Message();
        message.setToUser(phone);
        message.setType(phone);
        message.setIsRead(0);
        List<Message> messages = messageMapper.selectByRecord(message);
        messageMapper.updateReadMessage(phone);

        return messages;
    }
}
